/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client;

import edu.idp.client.draw.AdvancedDrawPanel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.FilteredImageSource;
import java.awt.image.RGBImageFilter;

/**
 *
 * @author Kristopher T Babic
 */
public class ToolOptionPanel extends Container implements MouseListener, ActionListener {

    private int option;
    private AdvancedDrawPanel drawingPanel;
    private Rectangle options[];
    private int set;
    private Image image1, image2, image3;

    public static final int POLY = 0;
    public static final int LINE = 1;
    public static final int NONE = 2;
    public static final int AIRBRUSH = 3;

    public ToolOptionPanel(AdvancedDrawPanel drawingPanel) {
//		set = 0;
        options = new Rectangle[3];
        this.drawingPanel = drawingPanel;

        options[0] = new Rectangle(5, 10, 60, 20);
        options[1] = new Rectangle(5, 40, 60, 20);
        options[2] = new Rectangle(5, 70, 60, 20);
        //options[3] = new Rectangle(5, 63, 60, 14);
        //setOption("P");

        addMouseListener(this);
    }

    public void setOption() {
        setOption(LINE);
    }

    public void setOption(int opt) {
        option = opt;
        set = 0;
        setOptionSelected(0);
    }

    private void setOptionSelected(int opt) {
        switch (option) {
            case POLY:
                if (set == 0) {
                    drawingPanel.setFill(false);
                    drawingPanel.setLine(true);
                }
                else if (set == 1) {
                    drawingPanel.setFill(true);
                    drawingPanel.setLine(true);
                }
                else if (set == 2) {
                    drawingPanel.setFill(true);
                    drawingPanel.setLine(false);
                }
                break;

            case LINE:
                if (set == 0)
                    drawingPanel.setDrawSize(1);
                else if (set == 1)
                    drawingPanel.setDrawSize(3);
                else
                    drawingPanel.setDrawSize(5);
                break;
            case AIRBRUSH:
                if (set == 0)
                    drawingPanel.setDrawSize(1);
                else if (set == 1)
                    drawingPanel.setDrawSize(2);
                else
                    drawingPanel.setDrawSize(3);
                break;

        }

        repaint();
    }

    public void paint(Graphics g) {
        paintDown(g);
        switch (option) {
            case POLY:
                paintPolyOpt(g);
                break;
            case LINE:
                paintLineOpt(g);
                break;
            case AIRBRUSH:
                paintAirBrushOpt(g);
                break;
            default:
                g.setColor(getBackground());
                g.drawRect(2, 2, getSize().width - 2, getSize().height - 2);
        }
    }

    private void paintPolyOpt(Graphics g) {
        g.setColor(new Color(220, 220, 200));
        g.fillRect(options[set].x, options[set].y, options[set].width, options[set].height);

        g.setColor(Color.black);
        g.drawRect(15, 15, 40, 10);
        g.drawRect(15, 45, 40, 10);

        g.setColor(new Color(230, 230, 230));
        g.fillRect(16, 46, 39, 9);
        g.fillRect(15, 75, 40, 10);
    }

    private void paintAirBrushOpt(Graphics g) {

        g.setColor(new Color(220, 220, 200));
        g.fillRect(options[set].x, options[set].y, options[set].width, options[set].height);

        if (image1 == null) {
            Image image1 = createImage(20, 20);
            Image image2 = createImage(20, 20);
            Image image3 = createImage(20, 20);

            System.out.println("creating images");
            Graphics gImage1 = image1.getGraphics();
            Graphics gImage2 = image2.getGraphics();
            Graphics gImage3 = image3.getGraphics();

            gImage1.setColor(Color.red);
            gImage2.setColor(Color.red);
            gImage3.setColor(Color.red);

            gImage1.fillRect(0, 0, 20, 20);
            gImage2.fillRect(0, 0, 20, 20);
            gImage3.fillRect(0, 0, 20, 20);

            gImage1.setColor(Color.black);
            gImage2.setColor(Color.black);
            gImage3.setColor(Color.black);

            for (int i = 0; i < 120; i++) {
                int x;
                int y;
                if (i < 30) {
                    x = (int) (Math.random() * edu.idp.client.draw.DrawAirBrush.RADIUS_1 * Math.sin(Math.random() * 360.0) + 10.);//35.);
                    y = (int) (Math.random() * edu.idp.client.draw.DrawAirBrush.RADIUS_1 * Math.cos(Math.random() * 360.0) + 10.);//20);
                    gImage1.drawLine(x, y, x, y);
                }
                if (i < 80) {
                    x = (int) (Math.random() * edu.idp.client.draw.DrawAirBrush.RADIUS_2 * Math.sin(Math.random() * 360.0) + 10.);//35.);
                    y = (int) (Math.random() * edu.idp.client.draw.DrawAirBrush.RADIUS_2 * Math.cos(Math.random() * 360.0) + 10.);//50.);
                    gImage2.drawLine(x, y, x, y);
                }
                x = (int) (Math.random() * edu.idp.client.draw.DrawAirBrush.RADIUS_3 * Math.sin(Math.random() * 360.0) + 10.);//35.);
                y = (int) (Math.random() * edu.idp.client.draw.DrawAirBrush.RADIUS_3 * Math.cos(Math.random() * 360.0) + 10.);//80.);
                gImage3.drawLine(x, y, x, y);
            }
            this.image1 = this.createImage(new FilteredImageSource(image1.getSource(), new ColorFilter(Color.red.getRGB() & 0x00ffffff)));
            this.image2 = this.createImage(new FilteredImageSource(image2.getSource(), new ColorFilter(Color.red.getRGB() & 0x00ffffff)));
            this.image3 = this.createImage(new FilteredImageSource(image3.getSource(), new ColorFilter(Color.red.getRGB() & 0x00ffffff)));

        }

        g.drawImage(image1, 25, 10, 20, 20, this);
        g.drawImage(image2, 25, 40, 20, 20, this);
        g.drawImage(image3, 25, 70, 20, 20, this);

    }

    private void paintLineOpt(Graphics graphic) {

        Graphics2D g = (Graphics2D) graphic;

        g.setColor(new Color(220, 220, 200));
        g.fillRect(options[set].x, options[set].y, options[set].width, options[set].height);

        g.setColor(Color.black);

        g.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
        g.drawLine(20, 20, 50, 20);

        g.setStroke(new BasicStroke(3, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
        g.drawLine(20, 50, 50, 50);

        g.setStroke(new BasicStroke(5, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
        g.drawLine(20, 80, 50, 80);

    }

    private void paintDown(Graphics g) {
//  System.out.println("paint down");
        Dimension size = getSize();
        g.setColor(Color.black);
        g.drawLine(0, 0, 0, size.height - 1);
        g.drawLine(1, 0, size.width - 1, 0);

        g.setColor(Color.darkGray);
        g.drawLine(1, 1, 1, size.height - 2);
        g.drawLine(2, 1, size.width - 2, 1);

        g.setColor(Color.white);
        g.drawLine(size.width - 1, 0, size.width - 1, size.height - 1);
        g.drawLine(0, size.height - 1, size.width - 2, size.height - 1);
        g.drawLine(size.width - 2, 1, size.width - 2, size.height - 2);
        g.drawLine(1, size.height - 2, size.width, size.height - 2);
    }

    public Dimension getMinimumSize() {
        return new Dimension(70, 100);
    }

    public Dimension getPreferredSize() {
        return new Dimension(70, 100);
    }

    public void mouseClicked(MouseEvent event) {
    }

    public void mouseReleased(MouseEvent event) {
    }

    public void mouseEntered(MouseEvent event) {
    }

    public void mouseExited(MouseEvent event) {
    }

    public void mousePressed(MouseEvent event) {
        //Graphics g = getGraphics();
        int x = event.getPoint().x;
        int y = event.getPoint().y;

        for (int i = 0; i < 3; i++) {
            if (options[i].contains(x, y)) {
                //g.setColor(new Color(220, 220, 200));
                //g.fillRect(options[i].x, options[i].y, options[i].width, options[i].height);
                set = i;
                setOptionSelected(i);
                break;
            }
            //else {
            //	g.setColor(Color.lightGray);
            //	g.fillRect(options[i].x, options[i].y, options[i].width, options[i].height);
            //}
        }
        //g.dispose();

    }

    public void actionPerformed(ActionEvent e) {
        int id = e.getID();
        setOption(id);
    }

    private final class ColorFilter extends RGBImageFilter {
        private int transColor;

        public ColorFilter(int transColor) {
            this.transColor = transColor;
            canFilterIndexColorModel = true;
        }

        public int filterRGB(int x, int y, int rgb) {
            if ((rgb & 0x00ffffff) == transColor)
                return rgb & 0x00ffffff;
            return rgb;
        }
    }

}
